﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
using System.IO;

using ThingyCloud.Creatures.Properties;

namespace ThingyCloud
{
	class XmlConnection
	{
		// All thingy configurations
		private HashSet<ThingyProperties> AllThingyProperties = new HashSet<ThingyProperties>();

		private static XmlConnection _instance;
		private Hashtable htSettings = new Hashtable();   //list with global settings
		private Hashtable htCrSettings = new Hashtable(); //list with all the creature settings
		private Hashtable htAvatarList = new Hashtable(); //List with the Avatar id's
		private ArrayList alThingyList = new ArrayList(); //List with the Thingy id's
		
		private XmlConnection()
		{
			Read(); 
		}

		public static XmlConnection getInstance()
		{
			if (null == _instance)
			{
				_instance = new XmlConnection();
			}
			return _instance;
		}

		public Hashtable getAvatarList()
		{
			return htAvatarList;
		}

		public Hashtable getCreatureSettings()
		{
			return htCrSettings;
		}

		public ArrayList getThingyList()
		{
			return alThingyList;
		}

		public string getSetting(string key)
		{
			string a = null;
			if (htSettings.ContainsKey(key))
			{
				a = (string)htSettings[key];
			}
			return a;
		}

		/// <summary>
		///  Reads the XML file and places al the Properties in Hashtables
		/// </summary>
		private void Read()
		{
			//Read global settings.
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load("GlobalSettings.xml");
			XmlNodeList lstNodes = xmlDoc.DocumentElement.ChildNodes;
			for (int i = 0; i < lstNodes.Count; i++)
			{
				htSettings.Add(lstNodes[i].Name, lstNodes[i].InnerText);
			}

			/*
			// Read creature settings
			XmlDocument doc = new XmlDocument();
			doc.LoadXml(Resources.Resources.CreatureSettings);

			// All creatures are under the root node
			XmlNodeList creatures = doc.DocumentElement.ChildNodes;

			// Process all creatures
			foreach (XmlNode node in creatures)
			{
				// Properties for this creature
				Hashtable properties = new Hashtable();

				// The creature type should be in an attribute 'type'
				string type = node.Attributes.GetNamedItem("type").InnerText;

				// Always has an id which can be cast to an integer
				int id = int.Parse(node.SelectNodes("id")[0].InnerText);
				properties.Add("id", id);

				Debug.WriteLine(string.Format("Found a creature w/ ID {0}", id));

				// Thingies have a speed
				if (type == "thingy")
				{	
					// Read and save speed
					int speed = int.Parse(node.SelectNodes("speed")[0].InnerText);

					// Thingy configuration!
					ThingyProperties props = new ThingyProperties(id, speed);

					// Thingies also have variants
					XmlNodeList variantNodes = node.SelectNodes("variants/variant");
					foreach (XmlNode variant in variantNodes)
					{
						// Read the ID and animation
						int variantId = int.Parse(variant.InnerText);
						string animation = variant.Attributes.GetNamedItem("animation").InnerText;

						// Create a new variant for this one and add it to the list of variants of the properties
						Variant var = new Variant(variantId, animation);
						props.Variants.Add(var);
					}

					AllThingyProperties.Add(props);
					Debug.WriteLine(props.ToString());
				}
			}
			*/

			/*
			// Creature properties reading starts here.
			xmlDoc = new XmlDocument();
			xmlDoc.Load(new StringReader(ThingyCloud.Resources.Resources.CreatureSettings));
			XmlNodeList lstCreatures = xmlDoc.DocumentElement.ChildNodes;

			for (int i = 0; i < lstCreatures.Count; i++)
			{
				Hashtable htProperties = new Hashtable();
				int creatureid = 0;
				XmlNodeList lstProperties = lstCreatures[i].ChildNodes;
				for (int y = 0; y < lstProperties.Count; y++)
				{
					string sKey = lstProperties[y].Name;
					string sValue = lstProperties[y].InnerText;
					if (sKey.Equals("id"))
					{
						creatureid = Globals.string2int(sValue);
					}
					if (!htProperties.ContainsKey(sKey))
					{
						htProperties.Add(sKey, sValue);
					}
				}

				if (lstCreatures[i].Name.Equals("thingy"))
				{
					alThingyList.Add(creatureid);
				}
				else if (lstCreatures[i].Name.Equals("avatar"))
				{
					if (htProperties.ContainsKey("weight"))
					{
						int iWeight = Globals.string2int((string)htProperties["weight"]);
						ArrayList alList = new ArrayList();
						if (htAvatarList.ContainsKey(iWeight))
						{
							alList = (ArrayList)htAvatarList[iWeight];
						}
						alList.Add(creatureid);
						htAvatarList[iWeight] = alList;
					}
				}
				htCrSettings.Add(creatureid, htProperties);
			}
			*/
		}
	}
}
